﻿
using System;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Model;
using System.Data.SQLite;

// ReSharper disable once CheckNamespace
namespace DAL
{
    public class SellerDal
    {

        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Seller");
            strSql.Append(" where ");
            strSql.Append(" id = @id  ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,8)			};
            parameters[0].Value = id;

            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Seller model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Seller(");
            strSql.Append("shopName,aliww,shopUrl,userName,pwd,type,addTime,lastLoginTime,tel,email,nickname");
            strSql.Append(") values (");
            strSql.Append("@shopName,@aliww,@shopUrl,@userName,@pwd,@type,@addTime,@lastLoginTime,@tel,@email,@nickname");
            strSql.Append(") ");

            SQLiteParameter[] parameters = {        
                        new SQLiteParameter("@shopName", DbType.String,100) ,            
                        new SQLiteParameter("@aliww", DbType.String,200) ,            
                        new SQLiteParameter("@shopUrl", DbType.String,500) ,            
                        new SQLiteParameter("@userName", DbType.String,50) ,            
                        new SQLiteParameter("@pwd", DbType.String,50) ,            
                        new SQLiteParameter("@type", DbType.Byte,1) ,            
                        new SQLiteParameter("@addTime", DbType.DateTime) ,            
                        new SQLiteParameter("@lastLoginTime", DbType.DateTime) ,            
                        new SQLiteParameter("@tel", DbType.String,20) ,            
                        new SQLiteParameter("@email", DbType.String,50),
                        new SQLiteParameter("@nickname", DbType.String,50)     
              
            };
            parameters[0].Value = model.ShopName;
            parameters[1].Value = model.Aliww;
            parameters[2].Value = model.ShopUrl;
            parameters[3].Value = model.UserName;
            parameters[4].Value = model.Pwd;
            parameters[5].Value = model.Type;
            parameters[6].Value = model.AddTime.ToString("s") ;
            parameters[7].Value = model.LastLoginTime.ToString("s");
            parameters[8].Value = model.Tel;
            parameters[9].Value = model.Email;
            parameters[10].Value = model.NickName;
            DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Seller model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Seller set ");

            strSql.Append(" id = @id , ");
            strSql.Append(" shopName = @shopName , ");
            strSql.Append(" aliww = @aliww , ");
            strSql.Append(" shopUrl = @shopUrl , ");
            strSql.Append(" userName = @userName , ");
            strSql.Append(" pwd = @pwd , ");
            strSql.Append(" type = @type , ");
            strSql.Append(" tel = @tel , ");
            strSql.Append(" email = @email,  ");
            strSql.Append(" nickname = @nickname  ");
            strSql.Append(" where id=@id  ");

            SQLiteParameter[] parameters = {
			            new SQLiteParameter("@id", DbType.Int32,8) ,            
                        new SQLiteParameter("@shopName", DbType.String,100) ,            
                        new SQLiteParameter("@aliww", DbType.String,200) ,            
                        new SQLiteParameter("@shopUrl", DbType.String,500) ,            
                        new SQLiteParameter("@userName", DbType.String,50) ,            
                        new SQLiteParameter("@pwd", DbType.String,50) ,            
                        new SQLiteParameter("@type", DbType.Byte,1) ,      
                        new SQLiteParameter("@tel", DbType.String,20) ,            
                        new SQLiteParameter("@email", DbType.String,50),
                        new SQLiteParameter("@nickname", DbType.String,50)   
              
            };

            parameters[0].Value = model.Id;
            parameters[1].Value = model.ShopName;
            parameters[2].Value = model.Aliww;
            parameters[3].Value = model.ShopUrl;
            parameters[4].Value = model.UserName;
            parameters[5].Value = model.Pwd;
            parameters[6].Value = model.Type;
            parameters[7].Value = model.Tel;
            parameters[8].Value = model.Email;
            parameters[9].Value = model.NickName;
            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            return false;

        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Seller ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,8)			};
            parameters[0].Value = id;


            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            return false;
        }



        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Seller GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id, shopName, aliww, shopUrl, userName, pwd, type, addTime, lastLoginTime, tel, email,nickname ");
            strSql.Append("  from Seller ");
            strSql.Append(" where id=@id ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@id", DbType.Int32,8)			};
            parameters[0].Value = id;


            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            return null;

        }

        /// <summary>
        /// DataRow转成实体对象
        /// </summary>
        public Seller DataRowToModel(DataRow dataRow)
        {
            Seller model = new Seller();
            if (dataRow["id"].ToString() != "")
            {
                model.Id = int.Parse(dataRow["id"].ToString());
            }
            model.ShopName = dataRow["shopName"].ToString();
            model.Aliww = dataRow["aliww"].ToString();
            model.ShopUrl = dataRow["shopUrl"].ToString();
            model.UserName = dataRow["userName"].ToString();
            model.Pwd = dataRow["pwd"].ToString();
            if (dataRow["type"].ToString() != "")
            {
                model.Type = int.Parse(dataRow["type"].ToString());
            }
            if (dataRow["addTime"].ToString() != "")
            {
                model.AddTime = DateTime.Parse(dataRow["addTime"].ToString());
            }
            if (dataRow["lastLoginTime"].ToString() != "")
            {
                model.LastLoginTime = DateTime.Parse(dataRow["lastLoginTime"].ToString());
            }
            model.Tel = dataRow["tel"].ToString();
            model.Email = dataRow["email"].ToString();
            model.NickName = dataRow["nickname"].ToString();
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataTable GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Seller ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataTable GetList(int top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (top > 0)
            {
                strSql.Append(" top " + top);
            }
            strSql.Append(" * ");
            strSql.Append(" FROM Seller ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQLite.Query(strSql.ToString()).Tables[0];
        }

        /// <summary>
        /// 验证用户名密码
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Seller ValidateLogin(string userName, string password)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id, shopName, aliww, shopUrl, userName, pwd, type, addTime, lastLoginTime, tel, email,nickname  ");
            strSql.Append("  from Seller ");
            strSql.Append(" where (userName=@userName or email=@userName) And pwd=@pwd");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@userName", DbType.String,30),
                    new SQLiteParameter("@pwd", DbType.String,30)
                                           };
            parameters[0].Value = userName;
            parameters[1].Value = password;

            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("update Seller set lastLoginTime='" + DateTime.Now.ToString("s") + "'");
                sb.Append(" where id=@id");
                SQLiteParameter[] param = {
					new SQLiteParameter("@id", DbType.Int32,8)			
                                          };
                
                param[0].Value = ds.Tables[0].Rows[0]["id"];

                //更新最后登录时间
                DbHelperSQLite.ExecuteSql(sb.ToString(), param);
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            return null;
        }
    }
}

